// This file is part of QIpMsg.
//
// QIpMsg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// QIpMsg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with QIpMsg.  If not, see <http://www.gnu.org/licenses/>.
//

#include "sound_thread.h"
#include "sound.h"

void SoundThread::run()
{
    forever {
        m_mutex.lock();
        QString path = m_soundFile;
        m_mutex.unlock();

        Sound sound(path);

        sound.play();

        m_mutex.lock();
        condition.wait(&m_mutex);
        m_mutex.unlock();

        if (abort) {
            return;
        }
    }
}

SoundThread::~SoundThread()
{
    m_mutex.lock();
    abort = true;
    condition.wakeOne();
    m_mutex.unlock();

    wait();
}

void SoundThread::play(QString path)
{
    QMutexLocker locker(&m_mutex);

    m_soundFile = path;

    if (!isRunning()) {
        start(LowPriority);
    } else {
        condition.wakeOne();
    }
}

